# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache Software License 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
# which is available at https://opensource.org/licenses/MIT.
#
# SPDX-License-Identifier: Apache-2.0 OR MIT

package(default_visibility = ["//visibility:public"])

load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_shared_library", "rust_static_library", "rust_test")

filegroup(
    name = "all_srcs",
    srcs = glob(["**"]),
)

filegroup(
    name = "cbindgen-cli",
    srcs = ["@cbindgen//file"],
)

genrule(
    name = "iceoryx2-ffi-cbindgen",
    srcs = glob(["src/**/*.rs"]) + [
        "//:all_srcs",
        "Cargo.toml",
        "cbindgen.toml",
        ":cbindgen-cli",
    ],
    outs = ["include/iox2/iceoryx2.h"],
    # this is actually just 'cbindgen iceoryx2-ffi/ffi --quiet --config iceoryx2-ffi/ffi/cbindgen.toml --output include/iox2/iceoryx2.h'
    cmd = "$(execpath :cbindgen-cli) $$(dirname $(location Cargo.toml)) --quiet --config $(location cbindgen.toml) --output $(OUTS)",
)

rust_shared_library(
    name = "iceoryx2-ffi-shared-rust",
    srcs = glob(["src/**/*.rs"]),
    deps = [
        "//iceoryx2:iceoryx2",
        "//iceoryx2-bb/container:iceoryx2-bb-container",
        "//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
        "//iceoryx2-bb/log:iceoryx2-bb-log",
        "//iceoryx2-bb/posix:iceoryx2-bb-posix",
        "//iceoryx2-bb/system-types:iceoryx2-bb-system-types",
        "//iceoryx2-cal:iceoryx2-cal",
        "@crate_index//:serde",
    ],
    tags = ["exclusive"],
    proc_macro_deps = [
        "//iceoryx2-bb/derive-macros:iceoryx2-bb-derive-macros",
        "//iceoryx2-ffi/ffi-macros:iceoryx2-ffi-macros",
    ],
)

rust_static_library(
    name = "iceoryx2-ffi-static-rust",
    srcs = glob(["src/**/*.rs"]),
    deps = [
        "//iceoryx2:iceoryx2",
        "//iceoryx2-bb/container:iceoryx2-bb-container",
        "//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
        "//iceoryx2-bb/log:iceoryx2-bb-log",
        "//iceoryx2-bb/posix:iceoryx2-bb-posix",
        "//iceoryx2-bb/system-types:iceoryx2-bb-system-types",
        "//iceoryx2-cal:iceoryx2-cal",
        "@crate_index//:serde",
    ],
    proc_macro_deps = [
        "//iceoryx2-bb/derive-macros:iceoryx2-bb-derive-macros",
        "//iceoryx2-ffi/ffi-macros:iceoryx2-ffi-macros",
    ],
)

# Combined library with header file

cc_library(
    name = "iceoryx2-ffi-shared",
    hdrs = [ "include/iox2/iceoryx2.h" ],
    includes = [ "include" ],
    deps = [
        ":iceoryx2-ffi-cbindgen",
        ":iceoryx2-ffi-shared-rust",
    ],
    linkstatic = False,
)

cc_library(
    name = "iceoryx2-ffi-static",
    hdrs = [ "include/iox2/iceoryx2.h" ],
    includes = [ "include" ],
    deps = [
        ":iceoryx2-ffi-cbindgen",
        ":iceoryx2-ffi-static-rust",
    ],
    linkstatic = True,
)

rust_test(
    name = "iceoryx2-ffi-tests",
    srcs = glob(["src/**/*.rs"]),
    deps = [
        "//iceoryx2:iceoryx2",
        "//iceoryx2-bb/container:iceoryx2-bb-container",
        "//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
        "//iceoryx2-bb/log:iceoryx2-bb-log",
        "//iceoryx2-bb/posix:iceoryx2-bb-posix",
        "//iceoryx2-bb/system-types:iceoryx2-bb-system-types",
        "//iceoryx2-bb/testing:iceoryx2-bb-testing",
        "//iceoryx2-cal:iceoryx2-cal",
    ],
    proc_macro_deps = [
        "//iceoryx2-bb/derive-macros:iceoryx2-bb-derive-macros",
        "//iceoryx2-ffi/ffi-macros:iceoryx2-ffi-macros",
        "@crate_index//:generic-tests",
    ],
)
